package com.example.newnoke.fragment.main;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.bumptech.glide.Glide;
import com.example.newnoke.R;
import com.example.newnoke.adapter.CommonAdapter;
import com.example.newnoke.adapter.ViewHolder;
import com.example.newnoke.data.BannerData;
import com.example.newnoke.data.LklListData;
import com.example.newnoke.data.NoticeData;
import com.example.newnoke.data.OrderData;
import com.example.newnoke.data.PayData;
import com.example.newnoke.data.QXData;
import com.example.newnoke.data.ZcbListData;
import com.example.newnoke.databinding.FragmentZichanBinding;
import com.example.newnoke.mvp.presenter.ZcPresenter;
import com.example.newnoke.mvp.view.IZcV;
import com.example.newnoke.payment.Payment;
import com.example.newnoke.ui.SucceedActivity;
import com.example.newnoke.ui.WebActivity;
import com.example.newnoke.utils.API;
import com.example.newnoke.utils.GlideImageLoader;
import com.example.newnoke.utils.GsonUtil;
import com.example.newnoke.view.dialog.PayDialgo;
import com.geya.jbase.basefragment.BaseDetailsFragment;
import com.geya.jbase.constant.RequestType;
import com.geya.jbase.uiview.LoadDialog;
import com.geya.jbase.uiview.ToastUtil;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by Administrator on 2018/5/22 0022.
 */

public class ZiChanFragment extends BaseDetailsFragment<ZcPresenter> implements IZcV, OnRefreshListener, OnLoadMoreListener {

    private FragmentZichanBinding mBinding;
    private int start = 1;

    @Override
    public ZcPresenter newPresenter() {
        return new ZcPresenter(this);
    }

    @Override
    public int inflateCreateView() {
        return R.layout.fragment_zichan;
    }

    @Override
    public void initDatas(View view) {

        mBinding = DataBindingUtil.bind(view);
        mBinding.swipeToLoadLayout.setOnRefreshListener(this);
        //为swipeToLoadLayout设置上拉加载更多监听者
        mBinding.swipeToLoadLayout.setOnLoadMoreListener(this);
        HashMap<String, String> map = new HashMap<>();
        map.put("start", start + "");
        map.put("limit", "10");
        map.put("type", "2");
        mPresenter.accessServers(RequestType.OKGO_POST, API.ORDER, OrderData.class, map);
//        initFlipper();
    }

    @Override
    protected void onFragmentStartLazy() {
        super.onFragmentStartLazy();
        mPresenter.accessServers(RequestType.OKGO_POST, API.IMAGES, BannerData.class, null);
        mPresenter.accessServers(RequestType.OKGO_POST, API.ZCB, ZcbListData.class, null);
        mPresenter.accessServers(RequestType.OKGO_POST, API.NOTICE, NoticeData.class, null);
    }



    @Override
    public void onRefresh() {
        start = 1;
        HashMap<String, String> map = new HashMap<>();
        map.put("start", start + "");
        map.put("limit", "10");
        map.put("type", "2");
        mPresenter.accessServers(RequestType.OKGO_POST, API.ORDER, OrderData.class, map);
        mPresenter.accessServers(RequestType.OKGO_POST, API.IMAGES, BannerData.class, null);
        mPresenter.accessServers(RequestType.OKGO_POST, API.ZCB, ZcbListData.class, null);
        mPresenter.accessServers(RequestType.OKGO_POST, API.NOTICE, NoticeData.class, null);
    }

    @Override
    public void onLoadMore() {
        start++;
        HashMap<String, String> map = new HashMap<>();
        map.put("start", start + "");
        map.put("limit", "10");
        map.put("type", "2");
        mPresenter.accessServers(RequestType.OKGO_POST, API.ORDER, OrderData.class, map);

    }


    private CommonAdapter<OrderData.DataBean> adapter;
    private List<OrderData.DataBean> mList = new ArrayList<>();

    @Override
    public void setListAdapter(List<OrderData.DataBean> list) {
        if (start == 1){
            mList.clear();
        }
        mList.addAll(list);
        if (adapter == null) {
            adapter = new CommonAdapter<OrderData.DataBean>(getActivity(), mList, R.layout.item_list_record) {
                @Override
                public void convert(ViewHolder holder, OrderData.DataBean s, int position) {
                    holder.setText(R.id.sj, s.getInputtime())
                            .setText(R.id.num, s.getTotal_num() + "")
                            .setText(R.id.type, s.getStatus() == 1 ? "已完成" : "未支付");
                }
            };
            mBinding.list.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }

        mBinding.swipeToLoadLayout.setRefreshing(false);
        mBinding.swipeToLoadLayout.setLoadingMore(false);
    }

    @Override
    public void pay(String s, final String order) {
        Payment mPayment = new Payment(getActivity());
        mPayment.setOnPayResult(new Payment.onPayResult() {
            @Override
            public void onSucceed() {
                startActivity(new Intent(getActivity(), SucceedActivity.class)
                        .putExtra("id", order)
                );
                ToastUtil.showShort("支付成功");
            }

            @Override
            public void onError() {
                ToastUtil.showShort("支付失败");
            }
        });
        mPayment.alipay(s);
    }

    @Override
    public void qxList(List<QXData.DataBean.ListBean> list) {

    }


    @Override
    public void setDatas(QXData.DataBean data) {

    }


    @Override
    public void setBanner(List<BannerData.DataBean> list) {

        List<String> imgs = new ArrayList<>();
        for (BannerData.DataBean bean : list) {
            imgs.add(RequestType.ADDRESS + bean.getThumb());
        }
        if (imgs.size() > 0) {
            mBinding.banner.setImages(imgs).setImageLoader(new GlideImageLoader()).setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {


                }
            }).start();
        }
    }

    @Override
    public void setZcbList(List<ZcbListData.DataBean> list) {
        CommonAdapter<ZcbListData.DataBean> adapter = new CommonAdapter<ZcbListData.DataBean>(getActivity(), list, R.layout.item_list_zichuan) {
            @Override
            public void convert(ViewHolder holder, final ZcbListData.DataBean dataBean, int position) {
                Glide.with(getActivity())
                        .load(RequestType.ADDRESS + dataBean.getThumb())
                        .into((ImageView) holder.getView(R.id.img));
                holder.setText(R.id.name, dataBean.getName())
                        .setOnClickListener(R.id.pay, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                PayDialgo dialgo = new PayDialgo();
                                Bundle bundle = new Bundle();
                                bundle.putString("name", dataBean.getName());
                                bundle.putString("price", dataBean.getPrice() + "".trim());
                                bundle.putString("type","2");

                                dialgo.setArguments(bundle);
                                dialgo.show(getActivity().getSupportFragmentManager(), new PayDialgo.PayCallBack() {
                                    @Override
                                    public void pay(String s1, String s2, String s3, String s4, String s5, String id) {
                                        LoadDialog.show(getActivity());
                                        HashMap<String, String> map = new HashMap<>();
                                        map.put("type", "2");
                                        map.put("goods_name", s2);
                                        map.put("total_num", s3);
                                        map.put("price", s4);
                                        map.put("payway", "1");
                                        mPresenter.accessServers(RequestType.OKGO_POST, API.PAY, PayData.class, map);
                                    }
//                    }
                                });
                            }
                        })
                ;

            }
        };

        mBinding.zcbList.setAdapter(adapter);

    }

    @Override
    public void setLklList(List<LklListData.DataBean> list) {

    }

    @Override
    public void initFlipper(List<NoticeData.DataBean> list) {

        if (list.size()==0){
            View views = getLayoutInflater().inflate(R.layout.item_zixun, null);
            TextView textView = views.findViewById(R.id.tv_text);
            textView.setText("暂无公告");

            mBinding.viewFlipper.addView(views);
        }else {
            for (final NoticeData.DataBean bean : list) {
                View views = getLayoutInflater().inflate(R.layout.item_zixun, null);
                TextView textView = views.findViewById(R.id.tv_text);
                textView.setText(bean.getTitle());
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(getActivity(),WebActivity.class)
                                .putExtra("title","公告详情")
                                .putExtra("url","file:///android_asset/article.html")
                                .putExtra("id",GsonUtil.GsonString(bean))
                        );
                    }
                });
                mBinding.viewFlipper.addView(views);
            }
        }
        mBinding.viewFlipper.setFlipInterval(2000);
        mBinding.viewFlipper.startFlipping();
    }


}
